//
// Created by 17775 on 2024/10/14.
//

#ifndef RAYCI_SNAPSHOT_H
#define RAYCI_SNAPSHOT_H

#include <QDockWidget>
#include "../qcustomplot.h"

QT_BEGIN_NAMESPACE
namespace Ui { class SnapShot; }
QT_END_NAMESPACE

class LiveMode_Measurement;
class Single_CrossSection;
class Single_CrossSectionUI;
class Single_PowerProfile;
class Single_Histogram;
class Single_HistogramUI;
class Single_PowerProfileUI;

class SnapShot : public QDockWidget {
Q_OBJECT

signals:
    void setAutoContrast(bool checked);
    void closeSnapshotWindow();
    void reportSnapShot();
    void showSnapShotOptions();

    void getCurrentY(double currentY);
    void updateEllipse(double x, double y, double radiusX, double radiusY, double angle);
    void showSnapShotPowerProfileOptions();
    void showSnapShotCrossSectionOptions();

//private slots:
//    void onMouseMove(const QMouseEvent *event) const;

private slots:
    void onMousePress(const QMouseEvent *event);
    void onMouseMove(const QMouseEvent *event);
    void onMouseRelease(const QMouseEvent *event);

public:
    explicit SnapShot(QWidget *parent = nullptr);
    ~SnapShot() override;
    void setWindowTitleName(const QString& name);
    Single_PowerProfileUI *singlePowerProfileUi = nullptr;
    static int nSizeX, nSizeY;
    void processCCDImageData(const QImage &image) const;
    void processFloatData(std::vector<float> &vecData);
    void setImageSize() const;
    void initEllipses(double dPositionX_px, double dPositionY_px, double dMajor_px, double dMinor_px) const;
    void setAuto(bool isAuto) const;

    // PowerProfile
    void paintPowerProfileEllipse(double x_center, double y_center, double major, double minor);
    void paintCenterPos(double x_center, double y_center) const;

private:
    Ui::SnapShot *ui;
    QCPItemPixmap *imageItem = nullptr;
    QCustomPlot *_2DImagePlot = nullptr;
    QCPItemLine *centerHLine = nullptr;
    QCPItemLine *centerVLine = nullptr;
    QCPItemLine *xCrossSectionLine = nullptr;
    QCPItemText *xCrossSectionText = nullptr;
    QCPItemEllipse *ellipse = nullptr;
    QCPItemText *coordinateLabel = nullptr;
    QList<QCPItemEllipse *> ellipses;
    QCPItemLine *hArrow = nullptr;
    QCPItemLine *vArrow = nullptr;
    QMenu *m_contextMenu = nullptr;
    QCPItemEllipse *powerProfileEllipse = nullptr;

    LiveMode_Measurement *liveModeMeasurement = nullptr;
    Single_CrossSection *singleCrossSection = nullptr;
    Single_CrossSectionUI *singleCrossSectionUi = nullptr;
    Single_PowerProfile *singlePowerProfile = nullptr;
    Single_Histogram *singleHistogram = nullptr;
    Single_HistogramUI *singleHistogramUi = nullptr;

    bool isDraggingXCrossSectionLine = false;
    bool isMoving = false;
    bool nearEllipseEdge = false;
    QPointF lastMousePos;
    double calculateDistanceToEllipseBoundary(const QPointF &mousePos) const;
    void set2DMapArea();
    void initCoordinateSystem();
    void btn_AOI_Click();
    void btn_Adjust_Click();
    void btn_3DView_Click();
    void btn_CrossSection_Click() const;
    void btn_PowerProfile_Click() const;
    void btn_Histogram_Click();
    void btn_BeamResults_Click();
    void btn_Report_Click();
    std::vector<float> _vecData;
    [[nodiscard]] float getDataAt(int x, int y) const;
    void closeEvent(QCloseEvent *event) override;
    bool bFirst = true;
    void resizeEvent(QResizeEvent *event) override;
};


#endif //RAYCI_SNAPSHOT_H
